#include "stdafx.h"

#include "GameConfiguration.h"

#include "XMLParsers.h"

#include "tinyxml2.h"

using namespace std;
using namespace tinyxml2;

namespace ZeEngine
{
    const std::string GameConfiguration::cDefaultCaption = "ZeEngine Game";
    const std::string GameConfiguration::cDefaultIcon = "";
    const bool GameConfiguration::cFullScren = false;
    const bool GameConfiguration::cShowMouse = true;

    bool GameConfiguration::LoadConfig(const std::string& iConfigFile)
    {
		bool fSuccess = false;

        m_pXMLConfig = new XMLParsers(iConfigFile);
    
        if (m_pXMLConfig && m_pXMLConfig->IsLoaded())
        {       
			fSuccess = true;

            m_pXMLConfig->SetPath("Configuration.Screen");
            SetSettingFromXml("Width", m_ScreenWidth);
            SetSettingFromXml("Height", m_ScreenHeight);
            SetSettingFromXml("BitsPerPixel", m_ScreenBPP);
            SetSettingFromXml("Title", m_Caption);
            SetSettingFromXml("Icon", m_Icon);

            m_FullScreen = SetBooleanFromXml("FullScreen");
            m_ShowMouse = SetBooleanFromXml("ShowMouse");
        
            m_pXMLConfig->SetPath("Configuration.Game");
            SetSettingFromXml("TicksPerSecond", m_TicksPerSecond);
            m_SkipTicks = 1000 / m_TicksPerSecond;

            m_pXMLConfig->SetPath("Configuration.Rendering");
            SetSettingFromXml("MaxFrameSkip", m_MaxFrameSkip);
        }
		else
		{
			LOG_ERROR("Error loading config file: " + iConfigFile);
		}

		return fSuccess;
    }

    bool GameConfiguration::SetBooleanFromXml(const std::string& iPath)
    {
        string s;
        SetSettingFromXml(iPath, s);

        if (s == "true")
        {
            return true;
        }
    
        return false;
    }

    GameConfiguration::GameConfiguration() : m_TicksPerSecond(cDefaultTicksPerSecond)
                                            ,m_SkipTicks     (cDefaultSkipTicks)
                                            ,m_MaxFrameSkip  (cDefaultMaxFrameSkip)
                                            ,m_ScreenWidth   (cDefaultScreenWidth)
                                            ,m_ScreenHeight  (cDefaultScreenHeight)
                                            ,m_ScreenBPP     (cDefaultScreenBPP)
                                            ,m_Caption       (cDefaultCaption)
                                            ,m_Icon          (cDefaultIcon)
                                            ,m_FullScreen    (cFullScren)
                                            ,m_ShowMouse     (cShowMouse)
                                            ,m_ScreenLogicalResolution(cDefaultLogicalResolutionX, cDefaultLogicalResolutionY)
    {
    }  

    GameConfiguration::~GameConfiguration()
    {
        Utilities::SafeDelete<XMLParsers>(m_pXMLConfig);
    }
}